<?php
namespace Home\Controller;
use Home\Controller\BaseController;
class ListController extends BaseController {
	public function _initialize(){
	}
    public function index(){
	 $meg=I('get.');
	 $this->assign('classmeg',$meg);
	 //var_dump($meg);
	  $sql="select * from goods where cid='{$meg['id']}' order by sum desc limit 0,3";
	  //echo $sql;	  
	  $phb=M()->query($sql);
	   $this->assign('phblist',$phb);//排行榜
	//var_dump($meg);exit;
	 parent::index("goods","cid",$meg['id'],$meg['mode'],$meg['dec']);//表,字段,数据,排序字段,升序/降序
    }
	public function news(){
		parent::index("news",null,null,"id","desc");
      }
	public function search(){
		$key=I('get.searchname');
		$mode=I('get.mode');
		$dec=I('get.dec');
		
		
		$clameg['mode']=$mode;
		$clameg['dec']=$dec;
		
		$this->assign("searchname",$key);
		$this->assign("classmeg",$clameg);//传递参数
		$this->perpage=12;
		//获取总记录数
		$sql="select count(*) from goods where name  like '%".$key."%'";
		//echo $sql;
		$count=M()->query($sql);		
		$cc=intval($count[0]['count(*)']);
		//var_dump($cc); exit;
		
		$Page = new \Think\Page($cc,$this->perpage);// 实例化分页类
		$show=$Page->show();// 分页显示输出
		
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性	
		if(!$mode)
			$sql1="select * from goods where name like '%".$key."%' order by id desc limit 0,".$Page->listRows;
		else 
			$sql1="select * from goods where name  like '%".$key."%' order by ".$mode." ".$dec." limit ".$Page->firstRow.",".$Page->listRows;
		
		//echo $sql1;exit;
		$list=M()->query($sql1);
		
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		
		
		 $sql2="select * from goods where name  like '%".$key."%' order by sum desc limit 0,3";
		//echo $sql;	  
		$phb=M()->query($sql2);
		$this->assign('phblist',$phb);//排行榜
		$this->display();	
	}
	public function shownew(){
		parent::mod("news","id");
	}
	public function showgood(){
		$id=I('get.id');		
		
		$cid=I('get.cid');
		$cname=I('get.cname');
		
		$this->assign("cid",$cid);
		$this->assign("cname",$cname);
		//所有信息
		$sql="select * from goods where id={$id}";
		//echo $sql;exit;
		$data1=M()->query($sql);
		$this->assign("good",$data1);
		
		 
		//分类信息
		$sql="select * from classfies where id='{$data1[0]['cid']}'";
		
		$data2=M()->query($sql);
		$this->assign("classname",$data2);
		
		//分数
		$sql="select avg(star) from scores where gid='{$data1[0]['id']}'";
		//echo $sql; exit;
		$data2=M()->query($sql);
		$ss=$data2['0']["avg(star)"];
		$this->assign("score",round($ss,2));
		//echo $ss;	exit;
		// 最新评论信息
		
		$mm=D("scores");
		$value=$data1[0]['id'];
		$count=$mm->where("gid= $value")->count();// 查询满足要求的总记录数			
		$Page = new \Think\Page($count,$this->perpage);// 实例化分页类
		$show=$Page->show();// 分页显示输出
		//进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$res = $mm->order("id desc")->where("gid = $value")->limit($Page->firstRow.','.$Page->listRows)->select();

		$this->assign("pnum",$count);
		$this->assign("meslist",$res);
		$this->display();
	}
		public function add(){//添加
			$data=I('post.');
			//print_r($data);exit;
			$comment=array(
				"content"=>$data["content"],
				"uid"=>$data["uid"],
				"vid"=>$data["vid"]
			);
			
			$scores=array(
				"uid"=>$data["uid"],
				"vid"=>$data["vid"],
				"score"=>$data['pscore']
			);
			
			$this->model=D("comments");
			if($this->model->create($comment)){//插入评论
				$this->model->add();
				//var_dump($comment);exit;
				$this->model=D("scores");
				if($this->model->create($scores)){//插入评分
					$this->model->add();
					$position="List/showgood/id/{$data['vid']}";
					//echo $position;
					$this->success('增加成功！',U($position));
				}
			}
	}
}	
